package edu.hawaii.ics414.blackjack.player;

import edu.hawaii.ics414.blackjack.card.Card;
import edu.hawaii.ics414.blackjack.card.GameDeck;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.junit.Test;

/**
 * Tests the BeginnerPlayer class.
 * 
 * @author Branden Ogata
 *
 */

public class TestBeginnerPlayer
{
  /**
   * Tests the methods of the BeginnerPlayer class.
   * Since TestPlayer tests all of the methods of the Seat interface,
   * these tests will only involve the methods specifically in the BeginnerPlayer class.
   * 
   */
  
  @Test
  public void test()
  {
    BeginnerPlayer beginner = new BeginnerPlayer(100);
    GameDeck deck = new GameDeck(6);
    
    // Test getCards for the BeginnerPlayer class
    beginner.startHand(deck);
    String cards = beginner.getCards();
    
    for (Card c : beginner.hands[0])
    {
      if (c != null)
      {
        assertTrue("Card should be included", cards.contains(c.toString()));
        cards = cards.substring(3);
      }
    }
    
    // Play a normal hand
    TestPlayer.makePlayableHand(beginner, deck);
    
    while ((beginner.sum(0) < 21) && (beginner.countNumberOfCards(0) <= 5) && (!beginner.stands[0]))
    {
      Action action = beginner.getMove(deck, 0, null);
      
      // Splits when possible
      if (beginner.canSplit(0))
      {
        assertEquals("Splits when possible", Action.SPLIT, action);
      }
      // Else should hit
      else if (beginner.canHit(0))
      {
        assertEquals("Hits if less than 21", Action.HIT, action);
      }
      else
      {
        assertEquals("Stands if unable to split or hit", Action.STAND, action);
      }
      
      beginner.play(deck, 0, action);
    }
    
    // Sum should now be 21 or greater, so should be standing
    assertEquals("Should stand after reaching 21", Action.STAND, beginner.getMove(deck, 0, null));
    
    // Test splitting
    beginner.clear();
    TestPlayer.makeSplittableHand(beginner, deck);
    assertEquals("Split when possible", Action.SPLIT, beginner.getMove(deck, 0, null));
    
    // Test with value of 20
    beginner.clear();
    while (beginner.canSplit(0))
    {
      TestPlayer.makeHandOfValue(beginner, deck, 20);
    }
    assertEquals("Hits with 20", Action.HIT, beginner.getMove(deck, 0, null));
    
    // Test with value of 21
    beginner.clear();
    TestPlayer.makeHandOfValue(beginner, deck, 21);
    assertEquals("Stands with 21", Action.STAND, beginner.getMove(deck, 0, null));
  }

}
